"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var crypto = require("crypto");
/*
* 主要负责对字符串进行加密,解密
* */
var MyCrypto = /** @class */ (function () {
    function MyCrypto(props) {
        this.password = '咖啡猫的密码';
        this.encode = 'utf8';
        if (props !== undefined) {
            var password = props.password, encode = props.encode;
            this.password = password;
            this.encode = encode;
        }
    }
    MyCrypto.prototype.dataEncode = function (DATA) {
        var keys = Object.keys(DATA);
        var encodeData = {};
        for (var i = 0, length_1 = keys.length; i < length_1; i++) {
            var key = keys[i];
            if (typeof DATA[key] !== 'string') {
                DATA[key] = String(DATA[key]);
            }
            encodeData[key] = this.strEncode(DATA[key]);
        }
        return encodeData;
    };
    MyCrypto.prototype.dataDecode = function (DATA) {
        var keys = Object.keys(DATA);
        var encodeData = {};
        for (var i = 0, length_2 = keys.length; i < length_2; i++) {
            var key = keys[i];
            encodeData[key] = this.strDecode(DATA[key]);
        }
        return encodeData;
    };
    /**
     * 字符串加密,便于存入数据库
     * @param str {string } 要加密的字符串
     * @return result {string} 返回加密过的字符串
     * */
    MyCrypto.prototype.strEncode = function (str) {
        var cipher1 = crypto.createCipher('aes192', this.password);
        /*
        * 第一个参数是要加密的字符串,第二个是输入编码,第三个是输出编码
        * */
        var encrypted = cipher1.update(str, 'utf8', 'hex');
        /*final函数返回加密后的数据*/
        encrypted += cipher1.final('hex');
        return encrypted;
    };
    ;
    /**
     * 字符串解密,便于前端使用
     * @param str {string }要解密的字符串
     * @return result {string} 返回解密过的字符串
     * */
    MyCrypto.prototype.strDecode = function (str) {
        var decipher = crypto.createDecipher('aes192', this.password);
        var decrypted = decipher.update(str, 'hex', 'utf8');
        decrypted += decipher.final('utf8');
        return decrypted;
    };
    ;
    return MyCrypto;
}());
exports.default = MyCrypto;
